package com.jzr.common.entity.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@ApiModel(description = "分页请求的返回模型")
public class ResultPage<T> extends Result<List<T>>{
    @ApiModelProperty(value = "当前页")
    private long pageNum;

    @ApiModelProperty(value = "每页的数量")
    private long pageSize;

    @ApiModelProperty(value = "数据总行数, 和count相同")
    private long total;

    @ApiModelProperty(value = "总页数")
    private long pages;

    @ApiModelProperty(value = "是否为第一页")
    private boolean isFirstPage = false;

    @ApiModelProperty(value = "是否为最后一页")
    private boolean isLastPage = false;

    private IPage<T> getNullPage(){
        IPage<T> page = new Page<>(1,20);
        page.setTotal(0);
        page.setPages(0);
        page.setSize(0);
        return page;
    }

    protected ResultPage(int code,String message,PageInfo<T> pageInfo){
        super(code,message,pageInfo.getList());
        this.pageNum = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.total = pageInfo.getTotal();
        this.count = pageInfo.getTotal();
        this.pages = pageInfo.getPages();
        this.isFirstPage = pageInfo.isIsFirstPage();
        this.isLastPage = pageInfo.isIsLastPage();
    }
    public static <T> ResultPage success(String message,PageInfo<T> pageInfo){
        return new ResultPage<>(0,message,pageInfo);
    }
    public static <T> ResultPage success(PageInfo<T> pageInfo){
        return new ResultPage<>(0,"成功",pageInfo);
    }
    public static ResultPage fail(String message){
        IPage<Object> page = null;
        return new ResultPage(-1,message,page);
    }

    protected ResultPage(int code, String message, IPage<T> page){
        super(code,message,page==null? null: page.getRecords());
        if(page == null) page = getNullPage();
        this.pageNum = page.getCurrent();
        this.pageSize = page.getSize();
        this.total = page.getTotal();
        this.count = page.getTotal();
        this.pages = page.getPages();
        this.isFirstPage = page.getCurrent() == 1;
        this.isLastPage = page.getCurrent()*page.getSize() >= page.getTotal();
    }
    protected ResultPage(int code, String message, IPage<T> page,List<T> records){
        super(code,message,records);
        if(page == null) page = getNullPage();
        this.pageNum = page.getCurrent();
        this.pageSize = page.getSize();
        this.total = page.getTotal();
        this.count = page.getTotal();
        this.pages = page.getPages();
        this.isFirstPage = page.getCurrent() == 1;
        this.isLastPage = page.getCurrent()*page.getSize() >= page.getTotal();
    }
    public static <T> ResultPage success(String message,IPage<T> page){
        return new ResultPage<>(0,message,page);
    }
    public static <T> ResultPage success(IPage<T> page){
        return new ResultPage<>(0,"成功",page);
    }
    public static <T> ResultPage success(String message,IPage<T> page,List<T> records){
        return new ResultPage(0,message,page,records);
    }
    public static <T> ResultPage success(IPage<T> page,List<T> records){
        return new ResultPage(0,"成功",page,records);
    }
}
